/*
 *  Things.js
 *  created by qinghong
 *  2016-7-11
 */

import React, { Component } from 'react';
import {
    AppRegistry,
    StyleSheet,
    Text,
    View,
    Navigator,
    Image,
    ScrollView
} from 'react-native';

import Moment from 'moment';

import NavBar from '../../Common/NavBar';
import Constants from '../../Common/Constants';
import OneUrls from '../../Common/OneUrls';
import ActivityView from '../../Common/ActivityView';
import ZanView from '../../Common/ZanView';


export default class Things extends Component {

    constructor(props) {
        super(props);
        this.state = {
            thingsData: null,
            loaded: false
        };
    }

    componentDidMount() {
        this.fetchData();
    }

    fetchData() {
        let date = Moment().format('YYYY-MM-DD');
        let urlStr = OneUrls.ThingsUrls.thingsData + '?' + 'strDate=' + date + '&strRow=1';
        fetch(urlStr)
            .then((res) => res.json())
            .then((resData) => {
                this.setState({
                    thingsData: resData.entTg,
                    loaded: true
                });
            })
            .done();
    }

    rightAction = () => {
        alert('弹出分享');
    };

    render() {

        if (!this.state.loaded) {
            return (
                <View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
                    <NavBar
                        titleIcon={require('../../images/navLogo.png')}
                        rightIcon={require('../../images/shareBtn.png')}
                        rightAction={this.rightAction}/>
                    <ActivityView text='正在加载东西数据...'/>
                </View>
            );

        } else {
            let dateStr = Moment().format('MMMM DD, YYYY');
            let imgUrl = this.state.thingsData.strBu;
            let title = this.state.thingsData.strTt;
            let content = this.state.thingsData.strTc;

            return (
                <View style={{justifyContent: 'center', alignItems: 'center'}}>
                    <NavBar
                        titleIcon={require('../../images/navLogo.png')}
                        rightIcon={require('../../images/shareBtn.png')}
                        rightAction={this.rightAction}
                    />
                    <ScrollView style={styles.scrollView}>
                        <Text style={{marginLeft: 10, marginTop: 10, fontSize: 12}}>{dateStr}</Text>
                        <Image source={{uri: imgUrl}}
                               style={styles.image}/>
                        <Text style={{fontSize: 17, marginTop: 15, marginLeft: 10}}>
                            {title}
                        </Text>
                        <Text style={{fontSize: 14, marginTop: 15, marginLeft: 10}}>
                            {content}
                        </Text>
                    </ScrollView>
                </View>
            );
        }
    }
}

const styles = StyleSheet.create({
    scrollView: {
        width: Constants.window.screenWidth,
        height: Constants.window.screenHeight - 64 - 49
    },
    image: {
        height: 350,
        justifyContent: 'center',
        marginLeft: 10,
        marginRight: 10,
        marginTop: 15
    }
});

